package com.baozi.order.controller;

import com.baozi.order.client.ProductClient;
import com.baozi.order.dto.CartDto;
import com.baozi.order.entity.product;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class ClientController {

      @Autowired
      private ProductClient productClient;

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;

//    @Autowired
//    RestTemplate restTemplate;

    @GetMapping("getServiceMsg")
    public String getServiceMsg() {

        String response="";
        //方式:1 直接RestTemplate写死url访问
        //RestTemplate restTemplate = new RestTemplate();
        // response = restTemplate.getForObject("http://localhost:8080/msg",String.class);

        //方式:2 loadBalancerClient通过在eureka上注册的名字url和端口，再RestTemplate
//        RestTemplate restTemplate = new RestTemplate();
//        ServiceInstance serviceInstance=loadBalancerClient.choose("PRODUCT");
//        String url=String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()+"/msg");
//        response = restTemplate.getForObject(url,String.class);

        //方式:3 使用LoadBalanced ，在restTemplate直接使用服务的名字
//        response=restTemplate.getForObject("http://PRODUCT/msg",String.class);

        //方法4用feign去访问

        response=productClient.getMsg();

        return response;
    }

    @GetMapping("getlist")
    public String getlist(){
        List<product> list=productClient.getProductList(Arrays.asList("157875196366160022","157875227953464068"));
        log.info("信息={}",list);
        return "ok";
    }

    @GetMapping("deleteStocke")
    public void deleteStocke(){
        CartDto cartdto= new  CartDto("157875196366160022",2);
        productClient.deleteStocke(Arrays.asList(cartdto));
    }


}
